package com.qiaotongtianxia.loans.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.CommonUtils;
import com.qiaotongtianxia.loans.util.Constants;
import com.qiaotongtianxia.loans.util.ToastUtil;
import com.qiaotongtianxia.loans.views.BaseEditText;
import com.qiaotongtianxia.loans.views.BaseTextView;
import com.qiaotongtianxia.loans.views.ButtonObserver;
import com.qiaotongtianxia.loans.views.DragLayout;
import com.qiaotongtianxia.loans.views.dialogs.ItemsDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/12/16.
 * 借款页面
 *
 * @author lisen < 4533548588@qq.com >
 */

public class LoansActivity extends BaseActivity implements DragLayout.DragListener{
    @BindView(R.id.tv_nav_title)
    BaseTextView tvNavTitle;
    @BindView(R.id.tv_notice)
    BaseTextView tv_notice;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;
    @BindView(R.id.btn_loans)
    ButtonObserver btnLoans;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.dragLayout)
    DragLayout dragLayout;

    private String [] hints = {"请输入您的真实姓名","请输入您的使用手机品牌","请输入您的手机号","请选择",
            "请输入手机服务密码","请输入手机号归属地","请输入您的支付宝账号",
            "请输入您的微信账号","请输入您的芝麻信用分","请输入您的花呗额度",
            "您是否有信用卡","请输入您的信用卡总额度","请输入您的工作单位名称",
            "请输入您的单位地址","请输入您的单位电话","请输入您的月收入","请输入您的现居住地址","请选择",
            "亲属关系","请输入第一个亲属姓名","请输入该亲属的手机号","亲属关系",
            "请输入第二个亲属姓名","请输入该亲属的手机号","请输入第一个朋友姓名",
            "请输入第一个朋友手机号","请输入第二个朋友姓名","请输入第二个朋友手机号",
            "请输入您要借款的金额"};
    private String[] tips = {"姓名","使用手机品牌","手机号","手机号是否本人实名认证且使用满6个月","服务密码","归属地"
            ,"支付宝账号","微信账号","芝麻信用分","花呗额度","是否有信用卡","信用卡总额度",
            "工作单位名称","单位地址","单位电话","月收入","现居住地址","是否结婚","亲属关系","亲属姓名",
            "亲属手机号","亲属关系","亲属姓名","亲属手机号","朋友姓名","朋友手机号","朋友姓名",
            "朋友手机号","借款金额"};
    private String[] parmas = {"name","phone_brand","mobile","isFullSix",
                                "service_no","belongto","alipay","wechat",
                                "sesame","huabei","have_credit","credit_max","company_name","company_add",
                                "company_no","salary","address","isMarry","relation1","relative1","mobile1",
                                "relation2","relative2","mobile2","friend1","mobile3","friend2","mobile4",
                                "amount"};
    private LayoutInflater inflater;
    private String loansAmount;

    private int top;
    private List<TextView> views = new ArrayList<>();//所有的有效文本框和输入框，用于提交数据

    @Override
    public int getLayoutId() {
        return R.layout.activity_loans;
    }

    @Override
    public Api onApiCreate() {
        return new Api(this);
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {
        dragLayout.setListener(this);
        inflater  = LayoutInflater.from(this);
        loansAmount = getIntent().getStringExtra(Constants.LOANS_AMOUNT);
        top = CommonUtils.dp2px(this,10f);
        initLayout();
        tv_notice.setFocusableInTouchMode(true);
        tv_notice.requestFocus();
    }

    private void initLayout() {
        for (int i = 0; i < 29; i++) {
            if (i== 3 || i == 10 || i == 17 || i ==18|| i == 21 || i == 28){//选择
                View view = inflater.inflate(R.layout.item_layout_choose, layoutContent, false);
                BaseTextView tvTip = (BaseTextView) view.findViewById(R.id.tv_chooseTip);
                BaseTextView tvResulte = (BaseTextView) view.findViewById(R.id.tv_chooseResult);
                tvTip.setText(tips[i]);
                tvResulte.setHint(hints[i]);
                view.setOnClickListener(new Listener(i));
                layoutContent.addView(view);
                btnLoans.observers(tvResulte);
                views.add(tvResulte);
                if (i == 3){
                    tvTip.setTextSize(12);
                    tvResulte.setTextSize(12);
                }
            }else{//输入框
                View view = inflater.inflate(R.layout.item_layout_content, layoutContent, false);
                BaseTextView tvTip = (BaseTextView) view.findViewById(R.id.tv_tip);
                final BaseEditText etResulte = (BaseEditText) view.findViewById(R.id.et_result);
                tvTip.setText(tips[i]);
                etResulte.setHint(hints[i]);
                layoutContent.addView(view);
                if (i == 2 || i == 4 || i == 8 || i == 9 || i == 11 || i == 14
                        || i == 15 || i ==20 || i == 23 || i == 25 || i == 27){//电话或数字
                    etResulte.setInputType(InputType.TYPE_CLASS_NUMBER);
                }else{
                    etResulte.setInputType(InputType.TYPE_CLASS_TEXT);
                }

                if (i == 5 || i == 12|| i == 13 || i ==16){
                    etResulte.setSingleLine(false);
                    etResulte.setFilters(new InputFilter[] { new InputFilter.LengthFilter(30) });
                }

                if (i != 11){
                    btnLoans.observers(etResulte);
                }
                views.add(etResulte);
            }
            if (i == 6 || i == 12 || i ==18 || i == 21 || i == 24 || i == 28){//空格
                View view = layoutContent.getChildAt(i);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
                params.setMargins(0,top,0,0);
                view.setLayoutParams(params);
            }
        }
        ViewGroup viewGroup = (ViewGroup) layoutContent.getChildAt(layoutContent.getChildCount()-1);
        BaseTextView textView = (BaseTextView) viewGroup.getChildAt(1);
        textView.setText(loansAmount);
        textView.setTag(loansAmount);
    }

    private class Listener implements View.OnClickListener{
        private int index = 0;
        public Listener(int index) {
            this.index = index;
        }
        @Override
        public void onClick(View v) {
            ItemsDialog.Buidler buidler = new ItemsDialog.Buidler(LoansActivity.this);
            if (index == 3){
                buidler.addItem("是","1");
                buidler.addItem("否","2");
            }else if (index == 10){
                buidler.addItem("有","1");
                buidler.addItem("无","2");
            }else if(index == 17){
                buidler.addItem("是","1");
                buidler.addItem("否","2");
            }else if (index == 18 || index == 21){
                buidler.addItem("父母","1");
                buidler.addItem("配偶","2");
                buidler.addItem("兄弟","3");
                buidler.addItem("姐妹","4");
            }else if (index == 28){
                for (int i = 0; i < 41; i++) {
                    buidler.addItem((100 * i+1000)+"",(100 * i+1000)+"");
                }
                buidler.setDialogHeight(CommonUtils.getScreenHeight(LoansActivity.this)/2);
            }
            buidler.setSimpleListener(new ItemsDialog.SimpleSheetListener() {
                @Override
                public void onSheetItemClick(int position, ItemsDialog.ItemBean itemBean) {
                    if (index == 10){
                        ViewGroup viewGroup = (ViewGroup) layoutContent.getChildAt(11);
                        if (position == 0){
                            viewGroup.setVisibility(View.VISIBLE);
                        }else{
                            viewGroup.setVisibility(View.GONE);
                            BaseEditText editText = (BaseEditText) viewGroup.getChildAt(1);
                            editText.setText("");
                        }
                    }
                    ViewGroup viewGroup = (ViewGroup) layoutContent.getChildAt(index);
                    BaseTextView textView = (BaseTextView) viewGroup.getChildAt(1);
                    textView.setText(itemBean.getName());
                    textView.setTag(itemBean.getT().toString());
                }
            });
            buidler.build();
        }
    }

    @OnClick({R.id.iv_nav_back, R.id.btn_loans})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_back:
                finish();
                break;
            case R.id.btn_loans:
                TreeMap<String,Object> map = new TreeMap<>();
                for (int i = 0; i < views.size(); i++) {
                    TextView textView = views.get(i);
                    if (textView instanceof EditText){
                        String result = textView.getText().toString();

                        if (textView.getVisibility()== View.VISIBLE){
                            if (!TextUtils.isEmpty(result)){
                                map.put(parmas[i],result);
                            }else{
                                map.put(parmas[i],"0");
                            }
                        }
                    }else {
                        map.put(parmas[i],textView.getTag().toString());
                    }
                }
                api.loans(map, new IBaseRequestImp<String>() {
                    @Override
                    public void onRequestSuccess(String s) {
                        ToastUtil.showShort(LoansActivity.this,s);
                        finish();
                    }
                });

                break;
        }
    }

    @Override
    public void initTitle() {
        tvNavTitle.setText(getString(R.string.loans));
    }

    @Override
    public boolean isCanPullDown() {
        return CommonUtils.isReachTop(scrollView);
    }

    @Override
    public boolean isCanPullUp() {
        return CommonUtils.isReachBottom(scrollView);
    }
}
